package com.example.Cairodio;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author Shokry
 * 
 *         This Activity shows all place that are nearby the user. The value
 *         which determines the nearby place exists in the CairodioDBAdapter
 *         Class. The list view is accessed from either the map view
 *         "Nearby Places" button, or from the notification when running in
 *         background
 * 
 */
public class NearbyPlacesActivity extends Activity {

	Vector<String> nearbyPlacesNames;
	CairodioDbAdapter dbHelper;
	double lat = 0, lon = 0;
	ListView listView;

	// Map button which navigates to the map view
	ImageButton map;

	// empty list used for handling exceptions, if the user is not nearby
	// anyplace
	static final Vector<String> emptyList = new Vector<String>();

	@Override
	/**
	 * @author Shokry
	 * 
	 * Fills the list view with places the are nearby using the method in CairodioDbAdapter.
	 * Then adds an action listener to the list view that navigates to the audio clips Activity
	 * when a place is clicked.
	 * 
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.places_list);

		// Stop receiving new coordinates
		stopService(CairodioMapActivity.intent);

		initUI();

		// Get Database
		dbHelper = ((CairodioApplication) getApplication()).getAdapter();

		Bundle b = getIntent().getExtras();

		if (b != null) { // To avoid incorrect starting of the activity

			lat = b.getDouble(getString(R.string.nearby_places_activity_latitude));
			lon = b.getDouble(getString(R.string.nearby_places_activity_longitude));
			nearbyPlacesNames = dbHelper.getNearByPlacesName(lat, lon);

			// If there are nearby places then display them, else display the
			// empty list
			if (nearbyPlacesNames.size() > 0) {

				ListAdapter myAdapter = new ListAdapter(this, nearbyPlacesNames);
				listView.setAdapter(myAdapter);

				listView.setOnItemClickListener(new OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {

						// Get the name of the place clicked
						String placeName = (String) ((TextView) (view
								.findViewById(R.id.clipName))).getText();
						int placeID = dbHelper.getID(placeName);

						// NAVIGATE TO AUDIO CLIPS
						Bundle b = new Bundle();
						b.putInt(
								getString(R.string.nearby_places_activity_place_id),
								placeID);
						b.putString(
								getString(R.string.nearby_places_activity_place_name),
								placeName);
						Intent intent = new Intent(NearbyPlacesActivity.this,
								AudioClipsActivity.class);
						intent.putExtras(b);
						startActivity(intent);

					}
				});
			} else {

				// display an empty list in case the bundle is empty
				ListAdapter myAdapter = new ListAdapter(this, emptyList);
				listView.setAdapter(myAdapter);

			}
		} else {
			// display an empty list in there are no places nearby
			ListAdapter myAdapter = new ListAdapter(this, emptyList);
			listView.setAdapter(myAdapter);
		}

	}

	/**
	 * @author Shokry
	 * 
	 *         initialize map button which navigates back to the map view, Title
	 *         of the view and empty list view
	 */
	private void initUI() {

		emptyList.add(getString(R.string.nearby_places_activity_empty_list));

		map = (ImageButton) findViewById(R.id.map);
		map.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent returnIntent = new Intent();
				setResult(RESULT_OK, returnIntent);
				finish();
			}
		});

		TextView place = (TextView) findViewById(R.id.Title);
		place.setText(R.string.nearby_places_activity_title);
		listView = (ListView) findViewById(R.id.PlacesListView);
		nearbyPlacesNames = new Vector<String>();
	}

	@Override
	public void onPause() {
		super.onPause();
		// intent = new Intent(CairodioMapActivity.this,
		// BackgroundService.class);
		startService(CairodioMapActivity.intent);
		// Toast.makeText(this,
		// "NearbyPlacesActivity: onPause",Toast.LENGTH_SHORT).show();
	}

	public void onRestart() {
		super.onRestart();
		stopService(CairodioMapActivity.intent);
		// Toast.makeText(this,
		// "NearbyPlacesActivity: onRestart",Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onStop() {
		super.onStop();
		// Toast.makeText(this,
		// "NearbyPlacesActivity: onStop",Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		stopService(CairodioMapActivity.intent);
		// Toast.makeText(this,
		// "NearbyPlacesActivity: onDestroy",Toast.LENGTH_SHORT).show();
	}

}